package com.hksj.Intelligentmanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.Intelligentmanagement.dto.SchedulingDTO;
import com.hksj.Intelligentmanagement.entity.SchedulingEntity;
import com.hksj.Intelligentmanagement.mapper.SchedulingMapper;
import com.hksj.Intelligentmanagement.vo.SchedulingVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName SchedulingService
 * @Description TODO
 * @Author @陈章
 * @Date 2023/3/30 15:53
 * @Version 1.0
 */
@Service
@Slf4j
public class SchedulingService extends ServiceImpl<SchedulingMapper, SchedulingEntity> {

    @Autowired
    private SchedulingMapper mapper;

    public void addScheduling(SchedulingDTO schedulingDTO) {
        SchedulingEntity schedulerEntity = new SchedulingEntity();
        BeanUtils.copyProperties(schedulingDTO, schedulerEntity);
        //通过手机号码查询微信表公众号的openId
        QueryWrapper<SchedulingEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("classes_name", schedulingDTO.getClassesName());
        SchedulingEntity schedulingEntity = mapper.selectOne(wrapper);
        if (schedulingEntity != null) {
            throw new RuntimeException("存在重复班次");
        }
        try {
            mapper.insert(schedulerEntity);
        } catch (Exception e) {
            throw new RuntimeException("新增异常");
        }

    }

    public void delScheduling(String id) {
        try {
            int i = mapper.deleteById(id);
            if (i <=0) {
                throw new RuntimeException("删除失败");
            }
        } catch (Exception e) {
            throw new RuntimeException("删除异常");
        }
    }

    public IPage<SchedulingVo> findAllScheduling(SchedulingDTO request) {
        Page<SchedulingVo> page = new Page<>(request.getPageNum(), request.getPageSize());
        List<SchedulingVo> userList = mapper.findAllScheduling(page, request);
        page.setRecords(userList);
        return page;
    }

    public void updateSchedulingById(SchedulingDTO schedulingDTO) {
        try {
            SchedulingEntity schedulerEntity = new SchedulingEntity();
            BeanUtils.copyProperties(schedulingDTO, schedulerEntity);
            mapper.updateById(schedulerEntity);
        } catch (Exception e) {
            throw new RuntimeException("更新异常");
        }
    }
}
